package com.hyyt.shopjsp.wxmg.fansMessage.controller;

import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.StringStaticKey;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import com.hyyt.shopjsp.util.redis.util.RedisDataReadUtil;
import com.hyyt.shopjsp.wxmg.basicInfo.pojo.WxGlobalReturnCode;
import com.hyyt.shopjsp.wxmg.fansMessage.pojo.WxLocationMessageInfo;
import com.hyyt.shopjsp.wxmg.fansMessage.service.IWxLocationMessageInfoService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright (c) 2016 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author wangyouchun on 2017/11/10 0026下午 4:10.
 */
@Controller
@RequestMapping("/back/wx/fansMessage/location")
public class WxLocationMessageInfoController extends BaseController {

    private Logger log = Logger.getLogger(WxLocationMessageInfoController.class);
    private String basePath = "/back/wxmg/fansMessage/fansLocation/";

    @Resource
    private IWxLocationMessageInfoService wxLocationMessageInfoService;
    /**
     * 进入粉丝位置消息页面
     *
     * @return 返回到指定页面
     */
    @RequestMapping("gotoLocationMessageInfoPage")
    public String gotoLocationMessageInfoPage() {
        return basePath + "fansLocationInfoList";
    }

    /**
     * 关联查询粉丝位置信息
     * @param beginTime
     * @param endTime
     * @param replyFlag
     * @param page
     * @param response
     */
    @RequestMapping(value="listLocationMessageInfo",method = RequestMethod.POST)
    @ResponseBody
    public void listLocationMessageInfo(String beginTime,String endTime,String replyFlag,String collectFlag ,Page page, HttpServletResponse response) throws ParseException {
        Map<String,Object> params=new HashMap<>();
        if(Utils.objectIsNotEmpty(beginTime)){
            params.put("beginTime",beginTime);
        }
        if(Utils.objectIsNotEmpty(endTime)){
            params.put("endTime",endTime);
        }
        int totalRecordCount = wxLocationMessageInfoService.countWxLocationMessageInfoMap(params);
        params.put("sortColumns","locationMessageInfoId desc");
        page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
        List<Map<String,Object>> wxImageMessageInfoListMap= wxLocationMessageInfoService.queryWxLocationMessageInfoMap(params,page.getPageRecordBeginIndex(), page.getPageSize());
        params.clear();
        params.put("total", totalRecordCount);// total键 存放总记录数，必须的
        params.put("rows", wxImageMessageInfoListMap);// rows键 存放每页记录 list
        ResultMsg.create(params, response);  //把内容发送到页面

    }

    /**
     * 获得粉丝位置消息对象
     * @param locationMessageInfoId
     * @return
     */
    @RequestMapping(value = "getLocationMessageInfoObject",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getLocationMessageInfoObject (Integer locationMessageInfoId,String wxName){
        Map<String,Object> params=new HashMap<>();
        if(Utils.objectIsNotEmpty(locationMessageInfoId)){
            WxLocationMessageInfo wxLocationMessageInfo = wxLocationMessageInfoService.loadWxLocationMessageInfo(locationMessageInfoId);
            if(Utils.objectIsNotEmpty(wxLocationMessageInfo)){
                params.put("wxLocationMessageInfo",wxLocationMessageInfo);
                params.put("wxName",wxName);
                return ResultMsg.create(true, null, FastJsonUtils.toJSONString(params));
            }
        }
        return  ResultMsg.create(false);
    }

    /**
     * 删除粉丝位置信息
     * @param locationMessageInfoId
     * @return
     */
    @RequestMapping(value = "deleteLocationInfo")
    @ResponseBody
    public ResultMsg deleteLocationInfo(Integer locationMessageInfoId){
        boolean flag=false;
        if(Utils.objectIsNotEmpty(locationMessageInfoId)){
            wxLocationMessageInfoService.deleteWxLocationMessageInfo(locationMessageInfoId);
            flag=true;
        }
        return ResultMsg.create(flag);
    }

    /**
     * 回复粉丝位置消息
     * @param wxName
     * @param sendMsgContent
     * @param wxLocationMessageInfo
     * @return
     */
    @RequestMapping(value = "saveReplyLocationMessage",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveReplyLocationMessage(String wxName,String sendMsgContent,WxLocationMessageInfo wxLocationMessageInfo){
        boolean result=false;
        String publicNoInfoStr = RedisDataReadUtil.getRedisValue(StringStaticKey.WX_PUBLIC_NO_INFO);
        Map<String,Object> publicNoInfo = FastJsonUtils.stringToCollect(publicNoInfoStr);
        String globalReturnCodeStr = RedisDataReadUtil.getRedisValue(StringStaticKey.WX_GLOBAL_RETURN_CODE);
        List<WxGlobalReturnCode> globalReturnCodeList = FastJsonUtils.toList(globalReturnCodeStr, WxGlobalReturnCode.class);
        String access_token = RedisDataReadUtil.getRedisValue(StringStaticKey.WX_ACCESS_TOKENS);
        String content=wxLocationMessageInfoService.saveReplyMessage(wxName,sendMsgContent, wxLocationMessageInfo,publicNoInfo,access_token,globalReturnCodeList);
        //判断返回值，1：true 正确 ，2：错误提示消息
        if("true".equals(content)){
            result=true;//1：true 正确
        }else{
            result=false;//2：错误提示消息
        }
        return ResultMsg.create(result,content);
    }
    
}